<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="clickTab">
      <el-tab-pane label="图片" name="4"></el-tab-pane>
      <el-tab-pane label="视频" name="1"></el-tab-pane>
      <el-tab-pane label="文档" name="3"></el-tab-pane>
    </el-tabs>
    <!-- 筛选 -->
    <div class="search">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="素材名称" prop="postCode">
          <el-input
            v-model="queryParams.resourceName"
            placeholder="请输入素材名称"
            clearable
            @keyup.enter.native="resetTable"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="resetTable"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
            >上传</el-button
          >
        </el-col>
      </el-row>
    </div>
    <!-- 列表 -->
    <div class="table">
      <el-table v-loading="loading" :data="pageList">
        <el-table-column
          label="资源名称"
          show-overflow-tooltip
          align="center"
          prop="resourceName"
        />
        <el-table-column label="资源大小" align="center" prop="resourceSize" />
        <el-table-column label="资源类型" align="center" prop="resourceType" />
        <el-table-column label="上传时间" align="center" prop="gmtCreate" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              >预览</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :total="total"
        :page.sync="queryParams.pageCurrent"
        :limit.sync="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </div>
    <add-resource
      ref="addResouce"
      :resourceType="+activeName"
      @refresh="resetQuery"
    />
    <add-resource-chunk
      ref="addResouceChunk"
      :resourceType="+activeName"
      @refresh="resetQuery"
    />
    <preview
      v-if="previewVisible"
      :type="+activeName"
      :src="activeItem.resourceUrl"
      @close="previewVisible = false"
    />
  </div>
</template>

<script>
import AddResource from "./addResource.vue";
import AddResourceChunk from "./addResourceChunk.vue";
import { QueryResourcePage, DeleteResource } from "@/api/course/material.js";
export default {
  name: "ResourceManage",
  components: {
    AddResource,
    AddResourceChunk,
  },
  data() {
    return {
      previewVisible: false,
      activeItem: {},
      loading: false,
      activeName: "4",
      total: 0,
      queryParams: {
        resourceType: "4",
        resourceName: "",
        pageCurrent: 1,
        pageSize: 10,
      },
      pageList: [],
      resourceTypeMap: {
        4: "图片",
        1: "视频",
        3: "文档",
      },
    };
  },
  methods: {
    handleView(row) {
      if (row.resourceType === this.resourceTypeMap[3]) {
        window.open("https://" + row.resourceUrl);
      } else {
        this.activeItem = row;
        if (
          this.activeItem.resourceUrl.indexOf("http") < 0 &&
          this.activeItem.resourceUrl.indexOf("https") < 0
        ) {
          this.activeItem.resourceUrl =
            "https://" + this.activeItem.resourceUrl;
        }
        this.previewVisible = true;
      }
    },
    clickTab() {
      this.queryParams.resourceType = +this.activeName;
      this.queryParams.resourceName = "";
      this.resetTable();
    },
    handleAdd() {
      if (+this.activeName !== 1) {
        this.$refs["addResouce"].show();
      } else {
        // 分片上传视频
        this.$refs["addResouceChunk"].show();
      }
    },
    handleQuery() {
      this.loading = true;
      QueryResourcePage(this.queryParams)
        .then((res) => {
          if (res.code === 200) {
            this.pageList = res.data.list.map((item) => {
              return {
                ...item,
                resourceSize: (item.resourceSize / 1024).toFixed(2) + "KB",
                resourceType: this.resourceTypeMap[item.resourceType],
              };
            });
            this.total = res.data.totalCount;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    resetTable() {
      this.queryParams.pageCurrent = 1;
      this.handleQuery();
    },
    resetQuery() {
      this.queryParams.resourceName = "";
      this.resetTable();
    },
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm(`是否确认删除名称 ${row.resourceName} 的素材项？`)
        .then(function () {
          return DeleteResource(id);
        })
        .then(() => {
          this.handleQuery();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.resetQuery();
  },
};
</script>

<style>
</style>